<?php
require(dirname(__FILE__).'/base.class.php');
class Slider extends Base{
    function __construct()
    {
        parent::__construct();
    }

    function index(){
        $output=['errcode'=>0,'msg'=>'success'];
        echo json_encode($output);
    }

    /*
        获取轮播图列表
    */ 
    function lists(){
        $output=['errcode'=>0,'msg'=>'success'];
         // 获取页码
         if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        $sql="select * from sliders";
        $sql .= " limit {$start},{$page_num}";
        $list=$this->db->query($sql);
        if(!empty($list)){
             // 将图片路径转换为绝对路径
             foreach($list as $key=>$item){
                 $list[$key]['img']=path($item['img']);
             }
             // 获取总记录数
             $sql = "select count(id) as count from sliders";
             $res = $this->db->get_one($sql);
             $total = $res['count'];
             $output['data'] = ['list' => $list, 'total' => $total];
        }else{
            $output['errcode']=1;
            $output['msg']='获取轮播图失败';
        }
        echo json_encode($output);
    }


    /*
        新增轮播图
    */ 
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            if (isset($_POST['img'])) {
                $data['img'] = $_POST['img'];
            }
            if (isset($_POST['url'])) {
                $data['url'] = $_POST['url'];
            }
            if (isset($_POST['page'])) {
                $data['page'] = $_POST['page'];
            }
            if (isset($_POST['status'])) {
                $data['status'] = $_POST['status'];
            }
            $query = arr2str($data);
            $sql = "insert into sliders set $query";
            $res = $this->db->query($sql);
            if ($res) {
                // 将轮播图id返回
                $output['id'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }

    /*
        编辑轮播图信息
    */ 
    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        $id = $_GET['id'];
        if ($_POST) {
            if (isset($_POST['img'])) {
                $data['img'] = $_POST['img'];
            }
            if (isset($_POST['url'])) {
                $data['url'] = $_POST['url'];
            }
            if (isset($_POST['page'])) {
                $data['page'] = $_POST['page'];
            }
            if (isset($_POST['status'])) {
                $data['status'] = $_POST['status'];
            }
            $query = arr2str($data);
            $sql = "update into sliders set $query where id={$id}";
            $res = $this->db->query($sql);
            if (!$res){
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }
    /*
        获取轮播图信息
    */
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "select * from sliders where id={$id}";
            $res = $this->db->get_one($sql);
            if (!empty($res)) {
                $res['img']=path($res['img']);
                $output['data'] = $res;
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
    /*
        删除轮播图
    */ 
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "delete from sliders where id={$id}";
            $res = $this->db->query($sql);
            if (!$res) {
                $output['errcode'] = 3;
                $output['msg'] = '删除失败';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

}
?>